package SelectSort;

import java.util.Arrays;

public class SelectSort {
    public static void main(String[] args) {
        int[] a = {78,45,1,34,54,322,345,79,6};
        selectSort(a);
        System.out.println(Arrays.toString(a));
    }
    //空间复杂度O(n^2)
    //空间复杂度O(1)
    //稳定性:不稳定
    private static void selectSort(int[] a) {

        for (int i = 0; i < a.length; i++) {
            int minindex = i;
            for (int j = i+1; j < a.length; j++) {
                if(a[minindex] > a[j]){
                    minindex = j;
                }
            }
            if(minindex != i) {
                swap(a, i, minindex);
            }
        }
    }

    private static void swap(int[] a, int i, int minindex) {
        int tmp = a[i];
        a[i] = a[minindex];
        a[minindex] = tmp;
    }
}
